﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Specialized;
using ToDoIt2.Model.Persistent.UI;

namespace ToDoIt2.UI {
	/// <summary>
	/// Interaction logic for TabControl.xaml
	/// </summary>
	public partial class TabControl : System.Windows.Controls.TabControl {
		protected App App { get { return (App.Current as App); } }

		public TabControl() : base() {
			InitializeComponent();

//			this.App.DocumentChanged += this.OnDocumentChanged;
//			this.OnDocumentChanged(this.App.Document);
		}

		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) {
			base.OnItemsChanged(e);
			switch (e.Action) {
				case NotifyCollectionChangedAction.Add:
					if (e.NewItems.Count == 1)
						this.SelectedValue = e.NewItems[0];
					break;
				case NotifyCollectionChangedAction.Reset:
					if (this.Items.Count > 0)
						this.SelectedIndex = 0;
					break;
			}
		}

		private void OnDocumentChanged(Model.Document Document) {
			this.DataContext = Document.Tabs;
		}
		protected override DependencyObject GetContainerForItemOverride() {
			return new UI.TabItem();
		}
	}
}
